<template>
  <div class="list-container">
    <el-divider>质量问题维护树</el-divider>
    <problem-tree
      v-if="qualityData.length"
      :data.sync="qualityData"
      :usual_drop="buildingDrops"
      :end_drop="houseDrops"
      :root_drop="rootDrops"
      :delete_node="deleteNode"
      :rename="rename"
      :add_node="addNode"
    ></problem-tree>
  </div>
</template>
<script>
  import ProblemTree from './components/ProblemTree.vue'
  import { getAllPart, add, deleteById, updatePart } from '@/api/problem'
  export default {
    name: 'Problem',
    components: { ProblemTree },
    data() {
      return {
        qualityData: [],
        buildingDrops: [
          {
            op: 'rename',
            value: '重命名',
          },
          {
            op: 'rm',
            value: '删除该节点',
          },
        ],
        houseDrops: [
          {
            op: 'rename',
            value: '重命名',
          },
          {
            op: 'rm',
            value: '删除该节点',
          },
        ],
        rootDrops: [
          {
            op: 'addCheckPoint',
            value: '添加问题项',
          },
        ],
      }
    },
    computed: {},
    created() {
      // this.fetchTree()
    },
    mounted() {
      this.fetchTree()
    },
    methods: {
      async deleteNode(id) {
        let push = {}
        push.faultId = id
        const { data: res } = await deleteById(push)
      },
      async rename(item) {
        const { data: res } = await updatePart(item)
      },
      async fetchTree() {
        const { data: res } = await getAllPart()
        let list = _.cloneDeep(res.body)
        this.qualityData = list.filter((l) => l.faultType == 1)
      },
      async addNode(node) {
        const { data: res } = await add(node)
      },
    },
  }
</script>
